#coding=utf-8
#导入包
import base64

from hyperlpr import *
#导入OpenCV库
import cv2
import numpy as np

def predict(base64Image):
    # 读入图片
    #cv_img = cv2.imdecode(np.fromfile("D:\\PlateDetect\\05_夜间\\皖M69311.jpg", dtype=np.uint8), -1)

    # 将byte转换成str，再转换成base64,去掉首部
    #image_path = "D:\\PlateDetect\\05_夜间\\皖M69311.jpg"
    #with open(image_path, 'rb') as f:
       #image = f.read()
    #image_base64 = str(base64.b64encode(image), encoding='utf-8')

    base642Mat = base64_to_image(base64Image)
    #cv2.imwrite("F:/car/test/1.png", base642Mat)
    #image_base64 = str(base64.b64encode(cv_img))

    # imgdata = base64.b64decode(image_base64)
    #print(base642Mat)
    #print(cv_img)

    # image = cv2.imread("D:\\PlateDetect\\05_夜间\\皖A00T45.jpg")
    # 识别结果
    return HyperLPR_plate_recognition(base642Mat)

def base64_to_image(base64_code):
    # base64解码
    img_data = base64.b64decode(base64_code)
    # 转换为np数组
    img_array = np.frombuffer(img_data, np.uint8)
    # 转换成opencv可用格式
    img = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR)

    return img
if __name__ == '__main__':
    #print(sys.argv)
    #print(predict(sys.argv[1]))
    image_path = "D:\\PlateDetect\\05_夜间\\皖M69311.jpg"
    with open(image_path, 'rb') as f:
        image = f.read()
    image_base64 = str(base64.b64encode(image), encoding='utf-8')
    #print(predict(image_base64))
    #print(predict(sys.argv[1]))
